package com.jtjrenren.android.taxi.driver.core.setting;

import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.geocode.GeoCodeOption;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.jtjrenren.android.taxi.driver.bean.api.ApiDutyInfo;
import com.jtjrenren.android.taxi.driver.R;
import com.jtjrenren.android.taxi.driver.bean.DriverInfo;
import com.jtjrenren.android.taxi.driver.bean.api.CommonResult;
import com.jtjrenren.android.taxi.driver.manger.AppManger;
import com.jtjrenren.android.taxi.driver.base.BaseActivity;
import com.jtjrenren.android.taxi.driver.manger.api.ApiWorks;
import com.library.weiget.ClearEditText;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * Created by Administrator on 2016/5/5.
 */
public class SettingFcActivity extends BaseActivity {
    @BindView(R.id.fc_ddmc)
    ClearEditText et_dd;
    @BindView(R.id.fc_fw)
    SeekBar seekBar;
    Button bt_save;

    private GeoCoder mSearch;
    private String city;
    private LatLng mLatLng;
    private String addr;


    public ApiDutyInfo info;
    private DriverInfo driver;

    @Override
    public int getLayoutID() {
        return R.layout.aty_setting_fc;
    }

    @Override
    public void init() {
        if (getIntent().hasExtra("info")) {
            info = (ApiDutyInfo) getIntent().getExtras().get("info");
        }

        driver = AppManger.getInstance().driver;

        ((TextView) findViewById(R.id.title_title)).setText("返程");
        findViewById(R.id.title_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        mSearch = GeoCoder.newInstance();
        mSearch.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() {
            @Override
            public void onGetGeoCodeResult(GeoCodeResult geoCodeResult) {
                if (geoCodeResult == null || geoCodeResult.error != SearchResult.ERRORNO.NO_ERROR) {
                    // 没有检索到结果
                    displayShort(getString(R.string.wuxiaodizhiqingchagnshijutishuru));
                    disProDialog();
                    return;
                }

                mLatLng = geoCodeResult.getLocation();

                setDuty();
            }

            @Override
            public void onGetReverseGeoCodeResult(ReverseGeoCodeResult reverseGeoCodeResult) {

            }
        });
        city = AppManger.getInstance().currentLocation.getCity();



        refreshView(info);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mSearch != null)
            mSearch.destroy();
    }

    @OnClick(R.id.fc_save)
    public void clickSave(){
        showProDialog("", getString(R.string.shezhitishi));

        addr = et_dd.getText().toString().trim();

        mSearch.geocode(new GeoCodeOption().city(city).address(addr));
    }

    private void refreshView(ApiDutyInfo info) {
        if (info == null)
            return;

        String[] split = info.getDestinationAddr().split("\\^");
        if (split != null && split.length > 0)
            et_dd.setText(split[0]);
        seekBar.setProgress(info.getSearchRadius());
    }

    private void setDuty() {
        addr = addr + "^" + mLatLng.latitude + "^" + mLatLng.longitude;
        int progress = seekBar.getProgress();
        if (driver == null)
            return;
        ApiWorks.setDriverRarelyDuty(driver.getWorkNo(), "1", "", addr, progress + "", "", "", new ApiWorks.ResponseListener<CommonResult>() {
            @Override
            public void onResponse(CommonResult commonResult) {
                disProDialog();
                if (commonResult != null && commonResult.getCode() == 1) {
                    displayShort(getString(R.string.shezhichenggong));
                    finish();
                } else {
                    displayShort(getString(R.string.shezhishibai));
                }
            }
        });
    }
}
